/** 
 * @(#)StringUtils.java Jun 3, 2010
 * 
 * Copyright (c) 1995-2010 Wonders Information Co.,Ltd. 
 * 1518 Lianhang Rd,Shanghai 201112.P.R.C.
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Wonders Group.
 * (Social Security Department). You shall not disclose such
 * Confidential Information and shall use it only in accordance with 
 * the terms of the license agreement you entered into with Wonders Group. 
 *
 * Distributable under GNU LGPL license by gnu.org
 */

package com.wondersgroup.wssip.util;

import java.util.StringTokenizer;

/**
 * @author xieguojun
 * @version $Revision$ Jun 3, 2010
 * @author ($Date$ modification by $Author$)
 * @since 1.0
 */
public class StringTools extends org.springframework.util.StringUtils {

	/**
	 * 比较str1大于str2.
	 * 
	 * @param str1
	 * @param str2
	 * @return str1>str2 返回true
	 */
	public static boolean greateThan(String str1, String str2) {

		if (str1 == null || str2 == null) {
			return false;
		}

		if (str1.compareTo(str2) > 0) {
			return true;
		}

		return false;

	}

	/**
	 * 比较str1少于str2.
	 * 
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean lessThan(String str1, String str2) {

		return greateThan(str2, str1);
	}

	/**
	 * 比较字符相等.
	 * 
	 * @param str1
	 * @param str2
	 * @return str1==str2 返回true
	 */
	public static boolean equalsstr(String str1, String str2) {

		return !greateThan(str1, str2) && !lessThan(str1, str2);

	}

	public static String copyByte(String src, int start, int length) {

		if (src == null) {
			return "";
		}

		byte[] srcData = src.getBytes();

		byte[] data = new byte[length];

		System.arraycopy(srcData, start, data, 0, length);

		return new String(data);

	}

	/**
	 * . author:xieguojun 分割字符串，忽略多个delim 例如多个空格，当作一个delim
	 * 
	 * @param str
	 * @param delim
	 * @return
	 */
	public static String[] split(String str, String delim) {
		StringTokenizer token = new StringTokenizer(str, delim);

		String[] result = new String[token.countTokens()];
		int loop = 0;

		while (token.hasMoreTokens()) {
			result[loop++] = token.nextToken();
		}

		return result;
	}

	public static void main(String args[]) {

		String str1 = "2";
		String str2 = "3";

		System.out.println(str1 + ">" + str2 + ":" + greateThan(str1, str2));
		System.out.println(str1 + "<" + str2 + ":" + lessThan(str1, str2));
		System.out.println(str1 + "=" + str2 + ":" + equalsstr(str1, str2));

		str2 = "2";

		System.out.println(str1 + ">" + str2 + ":" + greateThan(str1, str2));
		System.out.println(str1 + "<" + str2 + ":" + lessThan(str1, str2));
		System.out.println(str1 + "=" + str2 + ":" + equalsstr(str1, str2));
		str1 = "3";

		System.out.println(str1 + ">" + str2 + ":" + greateThan(str1, str2));
		System.out.println(str1 + "<" + str2 + ":" + lessThan(str1, str2));
		System.out.println(str1 + "=" + str2 + ":" + equalsstr(str1, str2));
	}

	public static boolean isEmpty(String str) {
		return ((str == null) || (str.length() == 0));
	}

	/**
	 * 字符串中子字符串个数.
	 * 
	 * @param str
	 * @param substr
	 * @return
	 */
	public static int count(String str, String substr) {
		if ((!(hasText(str))) || (!(hasText(substr)))) {
			return 0;
		}

		int count = 0;
		int index = 0;
		int pos = 0;
		int lenOfSubstr = substr.length();

		pos = str.indexOf(substr, index);
		while (pos != -1) {
			++count;
			index = pos + lenOfSubstr;
			pos = str.indexOf(substr, index);
		}
		return count;
	}
}
